<template>
  <!-- 拼团订单管理 -->
  <div class="app-container">
    <!-- 查询模块 -->
    <div class="formDiv">
      <el-form ref="form" :inline="true" :model="form" label-width="130px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="订单号">
              <el-input v-model="form.order_sn" clearable @keyup.enter.native="handleSearch" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="姓名">
              <el-input v-model="form.realname" clearable @keyup.enter.native="handleSearch" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="手机号">
              <el-input v-model="form.phone" clearable @keyup.enter.native="handleSearch" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级姓名">
              <el-input
                v-model="form.superior_realname"
                clearable
                @keyup.enter.native="handleSearch"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级手机号">
              <el-input v-model="form.superior_phone" clearable @keyup.enter.native="handleSearch" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单状态">
              <el-select
                v-model="form.order_status"
                placeholder="请选择"
                clearable
                @keyup.native="handleSearch"
              >
                <el-option
                  v-for="(item,index) in statusType"
                  :key="index"
                  :label="item.name"
                  :value="item.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="下单日期" width="105">
              <el-date-picker
                v-model="order_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="支付日期" width="236">
              <el-date-picker
                v-model="pay_time"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="核销日期" width="236">
              <el-date-picker
                v-model="verification_date"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6" :offset="18">
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
              <el-button type="primary" @click="handleExport">导出</el-button>
              <!-- <Exprotcomponent :params="form" url="admin/order/card_ticket_export" type="php"></Exprotcomponent> -->
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 主页列表 -->
    <div v-loading="tableLoading" class="listDiv">
      <el-table ref="tableData" :data="tableData" style="width: 100%" height="500px">
        <!--   -->
        <el-table-column
          fixed
          type="index"
          label="编号"
          width="50"
          align="center"
          :index="table_index"
        />
        <el-table-column label="订单编号" prop="order_id" align="center" width="200px" />
        <el-table-column label="姓名" prop="realname" align="center" />
        <el-table-column label="手机号" prop="username" align="center" width="120px" />
        <el-table-column label="上级姓名" prop="superior_realname" align="center" width="120px" />
        <el-table-column label="上级手机号" prop="superior_username" align="center" width="120px" />
        <el-table-column label="拼团名称" prop="project_name" align="center" width="120px" />
        <el-table-column label="拼团医院" prop="hospital_name" align="center" width="120px" />
        <el-table-column label="购买类型" prop="buy_type_name" align="center" width="120px" />
        <el-table-column label="订单状态" prop="status_name" align="center" width="120px">
          <template slot-scope="scope">
            <div>{{ scope.row.status_name }}</div>
          </template>
        </el-table-column>
        <el-table-column label="来源" prop="app_type_name" align="center" width="120px" />

        <el-table-column label="支付方式" prop="payment_name" align="center" width="120px" />
        <el-table-column label="原价" prop="project_original_price" align="center" width="120px" />
        <el-table-column label="拼团优惠" prop="group_discount_money" align="center" width="120px" />
        <el-table-column label="实付金额" prop="pay_amount" align="center" width="140px" />
        <el-table-column label="支付时间" prop="pay_date" align="center" width="200px">
          <template slot-scope="scope">
            <span v-if="scope.row.pay_date">{{ scope.row.pay_date }}</span>
            <!-- <span v-else></span> -->
          </template>
        </el-table-column>

        <el-table-column label="预约时间" prop="appointment_date" align="center" width="200px">
          <template slot-scope="scope">
            <span v-if="scope.row.appointment_date">{{ scope.row.appointment_date }}</span>
          </template>
        </el-table-column>
        <el-table-column label="核销时间" prop="verification_date" align="center" width="200px">
          <template slot-scope="scope">
            <span v-if="scope.row.verification_date">{{ scope.row.verification_date }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" fixed="right" align="center">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="dividedEtails(scope.row.id,scope.row)">提成详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pagesCon">
        <div>
          <el-pagination
            :current-page="form.page"
            :page-sizes="page_sizes"
            :page-size="form.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="page_totals"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </div>
    </div>
    <!-- 分成详情 -->
    <!-- <el-dialog
      title="分成详情"
      :visible.sync="dialogFormVisible"
      v-loading="infoLoading"
      width="90%"
      center
      class="dialogColor"
      @close="close"
    >
      <div class="content">
        <h5 class="titleInfo">分成详情</h5>
        <el-row>
          <el-col :span="16" style="background:white">
            <el-form
              ref="form1"
              :inline="true"
              label-position="top"
              :model="form1"
              label-width="150px"
            >
              <el-row>
                <el-col :span="8">
                  <el-form-item label="订单编号">
                    <span class="spanToInput">{{form1.detailInfo.order_sn | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="购买人姓名">
                    <span class="spanToInput">
                      <span
                        v-if="form1.detailInfo.buy_user"
                      >{{form1.detailInfo.buy_user.realname | noFn}}</span>
                    </span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="手机号">
                    <span class="spanToInput">
                      <span
                        v-if="form1.detailInfo.buy_user"
                      >{{form1.detailInfo.buy_user.referee | noFn}}</span>
                    </span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="购买数量">
                    <span class="spanToInput">{{form1.detailInfo.number | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="实付金额">
                    <span class="spanToInput">{{form1.detailInfo.order_price | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="订单生成时间">
                    <span class="spanToInput">{{(form1.detailInfo.created_at-0)*1000 | date(1)}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="分成发放时间">
                    <span class="spanToInput">{{form1.applyTime | noFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="订单状态">
                    <span class="spanToInput">{{form1.detailInfo.order_status | statusFn}}</span>
                  </el-form-item>
                </el-col>
                <el-col :span="8">
                  <el-form-item label="分成模式">
                    <span class="spanToInput">{{form1.formTypeValue | noFn}}</span>
                  </el-form-item>
                </el-col>
              </el-row>
            </el-form>
            <div class="line"></div>
            <h5 class="titleInfo">业绩奖励</h5>
            <el-form ref="form1" :inline="true" :model="form1" label-width="150px">
              <div v-if="form1.list1.length>0">
                <el-row v-for="(item,index) in form1.list1" :key="index">
                  <el-col :span="12">
                    <el-form-item
                      :label="item.awardType==16?'会员奖励':item.awardType==1?'一级人员':item.awardType==2?'二级人员':''"
                    >
                      <span class="spanToInput">{{item.realName}}{{item.username}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="人员级别">
                      <span class="spanToInput">{{item.roleName}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励比例(%)">
                      <span class="spanToInput">{{item.rewardRate | nullFn}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励金额">
                      <span class="spanToInput">{{item.amount}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-else class="fontColor">暂无</div>
            </el-form>
            <div class="line" />
            <h5 class="titleInfo">团队奖励</h5>
            <el-form ref="form1" :inline="true" :model="form1" label-width="150px">
              <div v-if="form1.list2.length>0">
                <el-row v-for="(item,index) in form1.list2" :key="index">
                  <el-col :span="12">
                    <el-form-item label="奖励人员">
                      <span class="spanToInput">{{item.realName}}{{item.username}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="人员级别">
                      <span class="spanToInput">{{item.roleName}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励比例(%)">
                      <span class="spanToInput">{{item.rewardRate | nullFn}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励金额">
                      <span class="spanToInput">{{item.amount}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-else class="fontColor">暂无</div>
            </el-form>
            <div class="line" />
            <h5 class="titleInfo">区域奖励</h5>
            <el-form ref="form1" :inline="true" :model="form1" label-width="150px">
              <div v-if="form1.list3.length>0">
                <el-row v-for="(item,index) in form1.list3" :key="index">
                  <el-col :span="12">
                    <el-form-item label="奖励人员">
                      <span class="spanToInput">{{item.realName}}{{item.username}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="人员级别">
                      <span class="spanToInput">{{item.roleName}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励比例(%)">
                      <span class="spanToInput">{{item.rewardRate | nullFn}}</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="奖励金额">
                      <span class="spanToInput">{{item.amount}}</span>
                    </el-form-item>
                  </el-col>
                </el-row>
              </div>
              <div v-else class="fontColor">暂无</div>
            </el-form>
          </el-col>
          <el-col :span="8">
            <div class="recommend">
              <div>当前推荐关系</div>
              <div v-if="userList.length>0">
                <div class="box" v-for="(item,index) in userList" :key="index">
                  <div class="boxContent">{{item}}</div>
                  <div class="arrow" v-if="index!=userList.length-1">↓</div>
                </div>
              </div>
              <div v-else class="mb_20">暂无</div>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-dialog> -->
    <!-- 分成详情弹窗 -->
    <el-dialog title="提成详情" :visible.sync="dividedDialogVisible" width="80%" center>
      <h5 class="titleInfo">提成详情</h5>
      <el-row>
        <el-col :span="16" style="background:white">
          <el-form
            ref="form1"
            :inline="true"
            label-position="top"
            :model="form1"
            label-width="200px"
          >
            <el-row>
              <el-col :span="8">
                <el-form-item label="订单编号">
                  <span class="spanToInput">{{ form1.detailInfo.order_id | noFn }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="购买人姓名">
                  <span class="spanToInput">
                    <span>{{ form1.detailInfo.realname | noFn }}</span>
                  </span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="手机号">
                  <span class="spanToInput">
                    <span>{{ form1.detailInfo.username | noFn }}</span>
                  </span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="购买数量">
                  <span class="spanToInput">{{ form1.detailInfo.number | noFn }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="实付金额">
                  <span class="spanToInput">{{ form1.detailInfo.pay_amount | noFn }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="订单生成时间">
                  <span class="spanToInput">{{ (form1.created_at) }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="分成发放时间">
                  <span class="spanToInput">{{ form1.applyTime | noFn }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="订单状态">
                  <span class="spanToInput">{{ form1.detailInfo.status_name }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="分成模式">
                  <span class="spanToInput">{{ form1.formTypeValue | noFn }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <div v-loading="tableLoading1" style="width:100%;">
                  <el-table ref="tableData1" :data="tableData1" border>
                    <el-table-column label="推荐人姓名" prop="realname" align="center" />
                    <el-table-column label="手机号" prop="username" align="center" />
                    <el-table-column label="推荐人级别" prop="role_name" align="center" />
                    <el-table-column label="奖励金额" prop="amount" align="center" />
                    <el-table-column label="奖励比率(%)" prop="dis_ratio" align="center" />
                    <el-table-column label="奖励类型" prop="msg" align="center">
                      <template slot-scope="scope">{{ scope.row.msg }}</template>
                    </el-table-column>
                    <el-table-column label="分成基数" prop="base_money" align="center" />
                    <el-table-column label="分成基数类型" prop="base_type" align="center" />
                    <el-table-column label="创建时间" prop="create_at" align="center" />
                  </el-table>
                </div>
              </el-col>
            </el-row>
          </el-form>
        </el-col>

        <el-col :span="8">
          <div class="recommend">
            <div>当前推荐关系</div>
            <div v-if="userList.length>0">
              <div v-for="(item,index) in userList" :key="index" class="box">
                <div class="boxContent">{{ item }}</div>
                <div v-if="index!=userList.length-1" class="arrow">↓</div>
              </div>
            </div>
            <div v-else class="mb_20">暂无</div>
          </div>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>
<script>
import serv from '@/utils/request1'
import { projectList, incomeTrans } from '@/api/NewRetailSystem'
import commonFn from '@/utils/common'
import { orderProjectExport } from '../../api/NewRetailSystem'
export default {
  name: 'GiftBagOrder',
  filters: {
    // 订单状态
    statusFn(opt) {
      switch (opt) {
        case 0:
          return '未支付'
          break
        case 1:
          return '已发货'
          break
        default:
          // return "暂无";
          break
      }
    },
    nullFn(opt) {
      if (opt) {
        return opt
      } else {
        return '--'
      }
    },
    // 无数据显示
    noFn(opt) {
      if (opt) {
        return opt
      } else {
        return ''
      }
    }
  },
  data() {
    return {
      http: serv.baseURLApp,
      verification_date:[],
      order_time: [],
      pay_time: [],
      form: {
        buy_type: 1,
        order_sn: '',
        realname: '',
        phone: '',
        superior_realname: '',
        superior_phone: '',
        order_status: '',
        order_time: [],
        pay_time: [],
        verification_date:[],
        page: 1,
        pagesize: 10
      },
      form1: {
        list1: [],
        list2: [],
        list3: [],
        listDetail: {},
        detailInfo: {},
        formTypeValue: '',
        applyTime: ''
      },
      userList: [],
      dividedInfo: [],
      type: '',
      placeDates: null,
      payDates: null,
      // 分页
      page_sizes: [10, 30, 50, 100], // 每页显示个数
      page_totals: 0, // 总条目数,
      statusType: [
        { name: '未支付', value: 0 },
        { name: '已支付', value: 1 },
        { name: '申请退款', value: 2 },
        { name: '已退款', value: 3 }
      ],
      tableData: [],
      tableData1: [], // 分成详情
      tableLoading: false,
      infoLoading: true,
      tableLoading1: true,
      dividedDialogVisible: false, // 分成
      dialogFormVisible: false // 女王卡优化分成
    }
  },
  watch: {
    // params(val){
    //   console.log(val)
    //   this.$emit("childfun", this.params);
    // }
    verification_date(val) {
      if (val) {
        this.form.verification_date = val;
      } else {
        this.form.verification_date = [];
      }
    },
    order_time(val) {
      if (val) {
        this.form.order_time = val
      } else {
        this.form.order_time = []
      }
    },
    pay_time(val) {
      if (val) {
        this.form.pay_time = val
      } else {
        this.form.pay_time = []
      }
    }
  },
  created() {
  },
  // 新加路由保存参数
  beforeRouteLeave(to, from, next) {
    // const answer = window.confirm("当前页面搜索条件将会被保存");
    // 更新 put
    const keyname = this.$route.name
    const value = JSON.stringify(this.form)
    sessionStorage.setItem(keyname, value)
    next()
  },
  mounted() {
    // 获取默认列表
    if (sessionStorage.getItem(this.$route.name)) {
      this.form = JSON.parse(sessionStorage.getItem(this.$route.name))
    }
    this.getList(this.form)
  },
  methods: {
    toRoutes(name) {
      const route = { name }
      this.$router.push(route)
    },
    // 序号
    table_index(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1
    },
    // 获取列表
    getList(e) {
      this.tableLoading = true
      projectList(e)
        .then(res => {
          console.log('xinagqing', res)
          if (res.data.code == 200) {
            this.tableData = res.data.data
            this.page_totals = res.data.total
            this.tableLoading = false
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: 'warning'
            })
          }
        })
        .catch(err => {
          this.errorStatus(err.msg)
          this.tableLoading = false
        })
    },

    // 分成详情
    getDetail(order_sn) {
      this.infoLoading = true
      console.log('order_sn', order_sn)
      const data = { order_sn: order_sn, order_type: 2 }
      incomeTrans(data)
        .then(res => {
          console.log('分成详情', res)
          if (res.data.code == 200 && res.data.data) {
            this.infoLoading = false
            this.tableLoading1 = false
            // this.form1.listDetail = res.data.data;
            this.form1.formTypeValue = res.data.data.split_mode
            this.form1.applyTime = res.data.data.split_time
            this.form1.created_at = res.data.data.created_at,
            this.form1.detailInfo.number = res.data.data.good_num

            // 奖励
            // let arr = [];
            // let arr1 = [];
            // let arr2 = [];
            // if (res.data.data) {
            //   res.data.data.income_list.forEach(item => {
            //     if (
            //       item.awardType == 16 ||
            //       item.awardType == 1 ||
            //       item.awardType == 2
            //     ) {
            //       arr.push(item);
            //     }
            //     if (item.awardType == 3) {
            //       arr1.push(item);
            //     }
            //     if (item.awardType == 8) {
            //       arr2.push(item);
            //     }
            //     this.form1.list1 = arr;
            //     this.form1.list2 = arr1;
            //     this.form1.list3 = arr2;
            //   });
            // } else {
            //   this.form1.list1 = [];
            //   this.form1.list2 = [];
            //   this.form1.list3 = [];
            // }
            this.tableData1 = res.data.data.income_list
              ? res.data.data.income_list
              : []
            // 分成用户链条
            this.userList = res.data.data.uidstr_list
              ? res.data.data.uidstr_list
              : []
          } else {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: 'warning'
            })
          }
        })
        .catch(err => {
          this.errorStatus('补贴单未复审!')
        })
    },
    // 分成详情弹窗
    dividedEtails(id, info) {
      console.log('info', info)
      this.form1.detailInfo = info
      const data = {
        orderId: id,
        type: 8
      }
      this.getDetail(info.order_id)
      // this.getUser(data);
      // this.detailInto(data);
      this.dividedDialogVisible = true
    },
    // 分成弹窗关闭事件
    close() {
      this.dividedDialogVisible = false
      this.form1 = {
        list1: [],
        list2: [],
        list3: [],
        listDetail: {},
        detailInfo: {}
      }
      this.userList = []
    },
    // 查询
    handleSearch() {
      this.form.page = 1
      this.getList(this.form)
    },

    // 导出
    handleExport() {
      const datas = { ...this.form }
      delete datas.page
      delete datas.pagesize
      orderProjectExport(datas)
        .then(res => {
          commonFn.downFile('拼团订单导出', res)
        })
        .catch(err => {
          this.errorStatus(err.msg)
        })
    },
    // 改变页面条数
    handleSizeChange(val) {
      this.form.pagesize = val
      this.getList(this.form)
    },
    // 选择页数
    handleCurrentChange(val) {
      this.form.page = val
      this.getList(this.form)
    },
    // // 弹窗组件
    eleMsg(txt, status) {
      this.$message({
        showClose: true,
        message: txt,
        type: status
      })
    },
    // 失败状态弹窗
    errorStatus(txt) {
      this.eleMsg(txt, 'error')
    }
  }
}
</script>
<style scoped>
.app-container {
  padding: 20px !important;
  background: transparent;
}
/* 主页列表样式 */
.listDiv {
  margin-bottom: 20px;
  padding: 0 20px;
}
.el-range-editor--medium.el-input__inner {
  width: 205px;
}
.pagesCon {
  text-align: right;
  margin-top: 20px;
}
.allCenter {
  text-align: center;
}
.spanToInput {
  width: 205px;
  min-height: 30px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  display: block;
  border: 1px solid #ddd;
  text-align: left;
  padding: 0 12px;
}
.line {
  height: 25px;
  background: #f7f7f7;
}
.recommend {
  min-width: 280px;
  color: #1d2023;
  text-align: center;
  font-size: 16px;
  margin-left: 25px;
}
.boxContent {
  margin-top: 20px;
  height: 25px;
  line-height: 25px;
  border: 1px solid #ddd;
  text-align: center;
  padding: 0 12px;
}
.arrow {
  text-align: center;
  padding: 12px 0 0 0;
}
.fontColor {
  font-size: 16px;
  text-align: center;
  margin-bottom: 15px;
}
.mb_20 {
  font-size: 20px;
  text-align: center;
  margin-top: 20px;
}
</style>
